Interactive Maps

Author

Aaron Kessler

Published

November 7, 2023

Interactive Maps with MapView Package

A h/t to Sharon Machlis for turning people on to the Mapview package, and her excellent walkthroughs of it.

For the data tonight we’ll be using the tidycensus package to pull both census data, as well as geospatial boundaries. Let’s quickly review how we use it.

census_api_key("2a6f8c21a30d3024e038d67d7d4eba647dc79cd4", overwrite=TRUE)
To install your API key for use in future sessions, run this function with `install = TRUE`.
#chose variables we want
myvars <- c(totalpop = "B01003_001",
            medincome = "B19013_001",
            medage = "B01002_001"
)
#pull for VA counties
va_counties_withgeo <- get_acs(geography = "county",
                       variables = c(myvars),
                       state = "VA",
                       output = "wide",
                       geometry = TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |======================================================================| 100%
va_counties_withgeo
Simple feature collection with 133 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -83.67539 ymin: 36.54074 xmax: -75.24247 ymax: 39.46601
Geodetic CRS:  NAD83
First 10 features:
   GEOID                         NAME totalpopE totalpopM medincomeE medincomeM
1  51105         Lee County, Virginia     22482        NA      37574       4430
2  51163  Rockbridge County, Virginia     22663        NA      57828       3580
3  51001    Accomack County, Virginia     33388        NA      50601       5803
4  51099 King George County, Virginia     26597        NA     101599       7080
5  51185    Tazewell County, Virginia     40615        NA      42937       3086
6  51029  Buckingham County, Virginia     16832        NA      49841       5835
7  51047    Culpeper County, Virginia     52021        NA      85274       5034
8  51179    Stafford County, Virginia    154204        NA     119818       3302
9  51085     Hanover County, Virginia    109232        NA      96911       2936
10 51009     Amherst County, Virginia     31381        NA      60876       3561
   medageE medageM                       geometry
1     45.6     0.7 MULTIPOLYGON (((-83.67461 3...
2     49.2     0.6 MULTIPOLYGON (((-79.68695 3...
3     47.1     0.6 MULTIPOLYGON (((-75.74241 3...
4     38.3     0.4 MULTIPOLYGON (((-77.34744 3...
5     45.1     0.3 MULTIPOLYGON (((-81.90089 3...
6     43.0     0.8 MULTIPOLYGON (((-78.83284 3...
7     39.0     0.6 MULTIPOLYGON (((-78.22926 3...
8     35.9     0.2 MULTIPOLYGON (((-77.63422 3...
9     42.1     0.4 MULTIPOLYGON (((-77.79681 3...
10    44.9     0.5 MULTIPOLYGON (((-79.43695 3...
#all counties in the US
all_counties_withgeo <- get_acs(geography = "county",
                       variables = c(myvars),
                       output = "wide",
                       geometry = TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
all_counties_withgeo
Simple feature collection with 3221 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -179.1489 ymin: 17.88328 xmax: 179.7785 ymax: 71.36516
Geodetic CRS:  NAD83
First 10 features:
   GEOID                              NAME totalpopE totalpopM medincomeE
1  20161              Riley County, Kansas     72602        NA      53296
2  19159             Ringgold County, Iowa      4739        NA      57700
3  30009            Carbon County, Montana     10488        NA      63178
4  16007           Bear Lake County, Idaho      6327        NA      60337
5  55011         Buffalo County, Wisconsin     13314        NA      61167
6  31185             York County, Nebraska     14164        NA      66337
7  08037            Eagle County, Colorado     55693        NA      91338
8  42129 Westmoreland County, Pennsylvania    355107        NA      64708
9  40079         Le Flore County, Oklahoma     48436        NA      43049
10 48053              Burnet County, Texas     48424        NA      65363
   medincomeM medageE medageM                       geometry
1        2489    25.5     0.1 MULTIPOLYGON (((-96.96095 3...
2        5058    44.3     1.0 MULTIPOLYGON (((-94.47167 4...
3        4261    50.7     0.9 MULTIPOLYGON (((-109.7987 4...
4        7039    38.9     1.1 MULTIPOLYGON (((-111.6345 4...
5        2352    46.5     0.5 MULTIPOLYGON (((-92.08384 4...
6        4128    39.5     1.2 MULTIPOLYGON (((-97.82629 4...
7        4058    37.8     0.8 MULTIPOLYGON (((-107.1137 3...
8        1350    47.1     0.2 MULTIPOLYGON (((-79.90487 4...
9        1869    38.8     0.3 MULTIPOLYGON (((-95.05996 3...
10       4694    44.7     0.3 MULTIPOLYGON (((-98.45924 3...
#remove MOE columns - they all end with "M"
va_counties_withgeo <- va_counties_withgeo %>%
  select(-ends_with("M"))

va_counties_withgeo
Simple feature collection with 133 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -83.67539 ymin: 36.54074 xmax: -75.24247 ymax: 39.46601
Geodetic CRS:  NAD83
First 10 features:
   GEOID                         NAME totalpopE medincomeE medageE
1  51105         Lee County, Virginia     22482      37574    45.6
2  51163  Rockbridge County, Virginia     22663      57828    49.2
3  51001    Accomack County, Virginia     33388      50601    47.1
4  51099 King George County, Virginia     26597     101599    38.3
5  51185    Tazewell County, Virginia     40615      42937    45.1
6  51029  Buckingham County, Virginia     16832      49841    43.0
7  51047    Culpeper County, Virginia     52021      85274    39.0
8  51179    Stafford County, Virginia    154204     119818    35.9
9  51085     Hanover County, Virginia    109232      96911    42.1
10 51009     Amherst County, Virginia     31381      60876    44.9
                         geometry
1  MULTIPOLYGON (((-83.67461 3...
2  MULTIPOLYGON (((-79.68695 3...
3  MULTIPOLYGON (((-75.74241 3...
4  MULTIPOLYGON (((-77.34744 3...
5  MULTIPOLYGON (((-81.90089 3...
6  MULTIPOLYGON (((-78.83284 3...
7  MULTIPOLYGON (((-78.22926 3...
8  MULTIPOLYGON (((-77.63422 3...
9  MULTIPOLYGON (((-77.79681 3...
10 MULTIPOLYGON (((-79.43695 3...
#remove that trailing "E"
colnames(va_counties_withgeo) <- sub("E$", "", colnames(va_counties_withgeo)) # $ means end of string only

va_counties_withgeo
Simple feature collection with 133 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -83.67539 ymin: 36.54074 xmax: -75.24247 ymax: 39.46601
Geodetic CRS:  NAD83
First 10 features:
   GEOID                          NAM totalpop medincome medage
1  51105         Lee County, Virginia    22482     37574   45.6
2  51163  Rockbridge County, Virginia    22663     57828   49.2
3  51001    Accomack County, Virginia    33388     50601   47.1
4  51099 King George County, Virginia    26597    101599   38.3
5  51185    Tazewell County, Virginia    40615     42937   45.1
6  51029  Buckingham County, Virginia    16832     49841   43.0
7  51047    Culpeper County, Virginia    52021     85274   39.0
8  51179    Stafford County, Virginia   154204    119818   35.9
9  51085     Hanover County, Virginia   109232     96911   42.1
10 51009     Amherst County, Virginia    31381     60876   44.9
                         geometry
1  MULTIPOLYGON (((-83.67461 3...
2  MULTIPOLYGON (((-79.68695 3...
3  MULTIPOLYGON (((-75.74241 3...
4  MULTIPOLYGON (((-77.34744 3...
5  MULTIPOLYGON (((-81.90089 3...
6  MULTIPOLYGON (((-78.83284 3...
7  MULTIPOLYGON (((-78.22926 3...
8  MULTIPOLYGON (((-77.63422 3...
9  MULTIPOLYGON (((-77.79681 3...
10 MULTIPOLYGON (((-79.43695 3...

Mapping Virginia counties with mapview

mapview(va_counties_withgeo, zcol = "medincome")

Customize colors

mapview(va_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (133)! 
Interpolating color vector to match number of zcol values.

This map’s dark background appeared automatically, because mapview determined the map included a lot of light colors. You can turn off that feature.

mapviewOptions("basemaps.color.shuffle" = FALSE)
mapview(va_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (133)! 
Interpolating color vector to match number of zcol values.

Two maps together

map_income <- mapview(va_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (133)! 
Interpolating color vector to match number of zcol values.
map_age <- mapview(va_counties_withgeo, zcol = "medage", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (94)! 
Interpolating color vector to match number of zcol values.
sync(map_income, map_age)

Side-by-side slider to compare the two, from the leaflet.extras2 package

map_income | map_age

To turn off legends, hover text, popups

mapview(va_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1,
         legend = FALSE, 
         label = FALSE, 
         popup = FALSE)
Warning: Found less unique colors (9) than unique zcol values (133)! 
Interpolating color vector to match number of zcol values.

Customize labels

mylabel <- glue::glue("{va_counties_withgeo$NAM} {va_counties_withgeo$medincome}")


mapview(va_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1,
         label = mylabel)
Warning: Found less unique colors (9) than unique zcol values (133)! 
Interpolating color vector to match number of zcol values.

Customize popups

mypopup <- glue::glue("<strong>{va_counties_withgeo$NAM}</strong><br />
                      Total Population: {va_counties_withgeo$totalpop}<br />
                      Median Income: {va_counties_withgeo$medincome}") %>% 
  lapply(htmltools::HTML)

# mylabel <- glue::glue("{all_data$State} {all_data$PctChange10_20}%") %>%
#   lapply(htmltools::HTML)
head(mypopup)
[[1]]
<strong>Lee County, Virginia</strong><br />
Total Population: 22482<br />
Median Income: 37574

[[2]]
<strong>Rockbridge County, Virginia</strong><br />
Total Population: 22663<br />
Median Income: 57828

[[3]]
<strong>Accomack County, Virginia</strong><br />
Total Population: 33388<br />
Median Income: 50601

[[4]]
<strong>King George County, Virginia</strong><br />
Total Population: 26597<br />
Median Income: 101599

[[5]]
<strong>Tazewell County, Virginia</strong><br />
Total Population: 40615<br />
Median Income: 42937

[[6]]
<strong>Buckingham County, Virginia</strong><br />
Total Population: 16832<br />
Median Income: 49841
mapview(va_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1,
         popup = mypopup)
Warning: Found less unique colors (9) than unique zcol values (133)! 
Interpolating color vector to match number of zcol values.